home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / ScreenDump / RubberBandit.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  3.2 KB  |  122 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        RubberBandit.c
  3.  
  4.     Contains:    Rubber Bandit is a simple srcXor rubber-banding example.
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 19911999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/14/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 9/23/94        nick            make RubberBandIt return the bounding rect, 
  21.                                             cleaned up discarded some fns
  22.  
  23. */
  24. #include <Types.h>
  25. #include <Quickdraw.h>
  26. #include <Events.h>
  27. // function prototypes:
  28.  
  29. Rect     RubberBandIt(Point anchorPt);
  30. Rect    *CheckRect(Rect *theRect);
  31. void    DrawStuff(Rect *rubberRect);
  32.  
  33.  
  34.  
  35. /*    ================================================
  36.     RubberBandIt follows the mouse and rubber-bands
  37.     a design based on its position.  It retains
  38.     control until the mouse button is released.
  39.     ================================================    */
  40.  
  41. Rect RubberBandIt(Point    anchorPt)
  42. {
  43.     Point    curMousePt = {0, 0};
  44.     Rect    rubberRect;
  45.  
  46.     PenMode(srcXor);                                /* Set pen mode to exclusive or.*/
  47.     PenPat(&qd.gray);
  48.  
  49.     GetMouse(&curMousePt);                            /* Get current mouse pos.        */
  50.  
  51.     rubberRect.top = anchorPt.v;                    /* Draw initial rectangle.        */
  52.     rubberRect.left = anchorPt.h;
  53.     rubberRect.bottom = curMousePt.v;
  54.     rubberRect.right = curMousePt.h;
  55.     DrawStuff(&rubberRect);
  56.  
  57.     while (Button())
  58.     {
  59.         GetMouse(&curMousePt);                        /* Get current mouse pos…        */
  60.  
  61.         if (curMousePt.h != rubberRect.right ||        /* If mouse moved…                */
  62.             curMousePt.v != rubberRect.bottom)
  63.         {
  64.             DrawStuff(&rubberRect);                    /* Erase old…                    */
  65.             rubberRect.bottom = curMousePt.v;
  66.             rubberRect.right = curMousePt.h;
  67.             DrawStuff(&rubberRect);                    /* and draw new.                */
  68.         }
  69.     }
  70.     
  71.     DrawStuff(&rubberRect);                            // Erase old at end.            */
  72.     return rubberRect ;
  73. }
  74.  
  75.  
  76. /*    ================================================
  77.     DrawStuff draws designs in the rectangle
  78.     passed, using the current pen mode, etc.
  79.     ================================================    */
  80.  
  81. void DrawStuff(rubberRect)
  82. Rect    *rubberRect;
  83. {
  84.     Rect    curRect;
  85.     
  86.     curRect = *rubberRect;
  87.     FrameRect(CheckRect(&curRect));
  88. }
  89.  
  90.  
  91.  
  92.  
  93. /*    ================================================
  94.     CheckRect makes sure that the top of the
  95.     rectangle passed is ≤ the bottom and the left is
  96.     ≤ the right.  FrameRect needs things this way.
  97.     ================================================    */
  98.  
  99. Rect *CheckRect(theRect)
  100. Rect    *theRect;
  101. {
  102.     short    temp;
  103.  
  104.     if (theRect->top > theRect->bottom)        /* Need to reverse top and bottom? */
  105.     {
  106.         temp = theRect->top;
  107.         theRect->top = theRect->bottom;
  108.         theRect->bottom = temp;
  109.     }
  110.  
  111.     if (theRect->left > theRect->right)        /* Need to reverse left and right? */
  112.     {
  113.         temp = theRect->left;
  114.         theRect->left = theRect->right;
  115.         theRect->right = temp;
  116.     }
  117.     
  118.     return theRect;                            /* This makes nested calls easier.    */
  119. }
  120.  
  121.  
  122.